
#pragma once

#include <string>
#include <unordered_map>
class ConnectManager;
class IoPacketPool;
class SSession;
class NetworkModule
{
public:
	NetworkModule();
	~NetworkModule();
	
	enum SIZE
	{
		MAX_IoTHREAD = 10,
		SIZE_WORKTHREAD = 1,
	};

	void initNetwork();
	bool Create();
	void Destroy();

	//factory for all session
	void ManagedSession(SSession* pSession);
	void UnManagedSession(SSession* pSession);
	void FreeManagedSession();

	//virtual method
	virtual SSession* GetNewSession();
	virtual SSession* GetSession(const int& id);

	static void Start()	{ms_bOpen=true;	}
	static void Stop()	{ms_bOpen=false;}


typedef unsigned int (__stdcall *THREAD_FUNC)(void* pArg);
	static HANDLE CreateNetThread(THREAD_FUNC pfRun, LPVOID arg, std::wstring name=L"netThread");

	static void CreateConnectThread();
	static void CreateWorkThread(const int& sizeOfWorkThread=SIZE_WORKTHREAD);
	static int ThreadWork();

	static bool ms_bOpen;
	static HANDLE ms_hIOCP;
	static int ms_sizeOfIoThread;
	static HANDLE ms_hIoThreads[ MAX_IoTHREAD ];
	static HANDLE ms_hConnectThread;
	
	static ConnectManager* ms_pConnectManager;
	static IoPacketPool* ms_pIoPacketPool;

private:
	int GenerateID() 
	{
		static int gs_id=0;
		return ++gs_id;
	}

	std::unordered_map<int, SSession*> m_mapSession;
	
};

unsigned int __stdcall gRunAcceptThread( void* pParam);
unsigned int __stdcall gWorkThread(void* pParam);
unsigned int __stdcall gConnectThread(void* pParam);
